package com.shapawan.tourism.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shapawan.tourism.common.Result;
import com.shapawan.tourism.entity.LocationType;
import com.shapawan.tourism.service.LocationTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 位置类型控制器
 */
@Api(tags = "位置类型管理")
@RestController
@RequestMapping("/api/locationTypes")
public class LocationTypeController {

    @Autowired
    private LocationTypeService locationTypeService;

    @ApiOperation("分页查询位置类型")
    @GetMapping("/page")
    public Result<Page<LocationType>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        
        Page<LocationType> page = new Page<>(current, size);
        LambdaQueryWrapper<LocationType> queryWrapper = new LambdaQueryWrapper<>();
        
        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.like(LocationType::getName, keyword);
        }
        
        queryWrapper.orderByAsc(LocationType::getSortOrder);
        
        return Result.success(locationTypeService.page(page, queryWrapper));
    }

    @ApiOperation("获取所有启用的位置类型")
    @GetMapping("/list")
    public Result<List<LocationType>> list() {
        return Result.success(locationTypeService.getEnabledTypes());
    }

    @ApiOperation("根据ID获取位置类型")
    @GetMapping("/{id}")
    public Result<LocationType> getById(@PathVariable Integer id) {
        return Result.success(locationTypeService.getById(id));
    }

    @ApiOperation("新增位置类型")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<LocationType> add(@Validated @RequestBody LocationType locationType) {
        locationTypeService.save(locationType);
        return Result.success(locationType);
    }

    @ApiOperation("修改位置类型")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> update(@PathVariable Integer id, @Validated @RequestBody LocationType locationType) {
        locationType.setId(id);
        locationTypeService.updateById(locationType);
        return Result.success("修改成功");
    }

    @ApiOperation("删除位置类型")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<String> delete(@PathVariable Integer id) {
        locationTypeService.removeById(id);
        return Result.success("删除成功");
    }
} 